package Game;

import java.util.Random;
import java.util.Scanner;
import java.util.Vector;

import GameElement.GameCard;
import GameElement.GameIO;
import GameElement.GamePlayer;


public class EventExecution {
	private GamePlayer[] game_players;		// Create a array to store all game players
	private GamePlayer current_player;		// Create a variable to identify current turn player 
	private GameIO gameIO;					// Create a variable of game input/output
	private Vector card_deck;				// Create a vector list to store all cards
	
	/**
	 * Identify which player execute event
	 * Execute card event and the card effect on the other players
	 * @param current_player
	 * @param game_players
	 * @param card_deck
	 * @param gameIO
	 */
	public EventExecution(GamePlayer current_player, GamePlayer[] game_players, Vector card_deck, GameIO gameIO) {
		this.current_player = current_player;
		this.game_players = game_players;
		this.card_deck = card_deck;
		this.gameIO = gameIO;
		executeEvent();
	}
	
	/**
	 * Check the player position
	 * If position = 0  ->	player add 300 scores
	 * If position = 10 ->	player move 3 step forward, draw card and execute event.
	 * If position = 20 ->	player minus. 100 scores
	 * If position = 30 ->	player move 3 step backward, draw card and execute event.
	 * Default			->	draw card and execute event.
	 */
	public void executeEvent() {
		gameIO.printEvent(current_player.getPosition());
		switch (current_player.getPosition()) {
			case 0:
				current_player.modifyScore(300);
				break;
			case 10:
				current_player.modifyPosition(3);
				executeCardEvent(card_deck.size());
				break;
			case 20:
				current_player.modifyScore(-100);
				break;
			case 30:
				current_player.modifyPosition(-3);
				executeCardEvent(card_deck.size());
				break;
			default:
				executeCardEvent(card_deck.size());
				break;
		}
	}
	
	/**
	 * Draw Card form card deck and execute from different card type
	 * @param randomCount
	 */
	public void executeCardEvent(int randomCount) {
		Random random = new Random();
		int cardNum = random.nextInt(randomCount);
		GameCard c = (GameCard) card_deck.get(cardNum);
		if(c.getType().equals("ACT")){
			executeActCard(c);
		}else if(c.getType().equals("EVENT")){
			executeEventCard(c);
		}else if(c.getType().equals("SAY")){
			exeucteSayCard(c);
		}
	}
	
	/**
	 * Execute act card event
	 * If answer is correct 	-> 	get score
	 * If answer is incorrect 	->	deduce score
	 * @param c
	 */
	public void executeActCard(GameCard c){
		String answer =  gameIO.getActCardAnswerFromPlayer(c);
		int originScore = current_player.getScore();
		int afterScore = 0;
		if(answer.equals("A")){
			current_player.modifyScore(c.getCardSocre()[0][0]);
		}else if(answer.equals("R")){
			current_player.modifyScore((0-c.getCardSocre()[0][0]));
		}
		afterScore = current_player.getScore();
		if((afterScore-originScore)>0){
			gameIO.printChoseCorrectAnswer(c.getCardSocre()[0][0]);
			current_player.modifyInteractionScore(10);
		}else{
			gameIO.printChoseWrongAnswer(c.getCardSocre()[0][0]);
			current_player.modifyInteractionScore(-10);
		}
	}

	/**
	 * Execute event card event
	 * If answer is correct 	-> 	get score
	 * If answer is incorrect 	->	deduce score
	 * @param c
	 */
	public void executeEventCard(GameCard c){
		int role = 0;
		String[] game_player_roles = { "SON", "DAUG.", "FATHER", "MOTHER",
				"GRANDMA", "GRANDPA" };
		for(int i=0; i<game_player_roles.length; i++){
			if (current_player.getRoles().equals(game_player_roles[i]))
				role = i;
		}
		int selected_answer = gameIO.getEventCardAnswerFromPlayer(c,role);
		current_player.modifyScore(c.getCardSocre()[role][selected_answer]);
		if(c.getCardSocre()[role][selected_answer]<=0){
			gameIO.printChoseWrongAnswer(c.getCardSocre()[role][selected_answer]);
			current_player.modifyUnderstandingScore(-10);
		}else{
			gameIO.printChoseCorrectAnswer(c.getCardSocre()[role][selected_answer]);
			current_player.modifyUnderstandingScore(10);
		}
	}
	
	/**
	 * Execute say card event
	 * If answer is correct 	-> 	get score
	 * If answer is incorrect 	->	deduce score
	 * @param c
	 */
	public void exeucteSayCard(GameCard c){
		String answer =  gameIO.getSayCardAnswerFromPlayer(c);
		if(answer.equals("A")){
			gameIO.printChoseCorrectAnswer(c.getCardSocre()[0][0]);
			current_player.modifyScore(c.getCardSocre()[0][0]);
			current_player.modifyCommunicationScore(10);
			for(int i=0; i<game_players.length; i++){
				if(!(game_players[i]==current_player)){
					String answer2 = gameIO.getOtherPlayersFeedback(c, current_player, game_players, i);
					if(answer2.equals("Y")){
						gameIO.printChoseCorrectAnswer(c.getCardSocre()[0][0]/2);
						game_players[i].modifyScore(c.getCardSocre()[0][0]/2);
						game_players[i].modifyCommunicationScore(5);
					}else if (answer2.equals("N")){
						gameIO.printChoseWrongAnswer(c.getCardSocre()[0][0]/2);
						game_players[i].modifyScore(0-c.getCardSocre()[0][0]/2);
						game_players[i].modifyCommunicationScore(0-5);
					}
				}
			}
		}else if(answer.equals("R")){
			gameIO.printChoseWrongAnswer(c.getCardSocre()[0][0]);
			current_player.modifyScore((0-c.getCardSocre()[0][0]));
			current_player.modifyCommunicationScore(-10);
		}
	}
}
